﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoseaBussinesQA;
using System.Configuration;
using System.Data.SqlClient;

public partial class WebFormatoDoc1 : System.Web.UI.Page
{
    private static AppSettingsReader settings = new AppSettingsReader();
    string cadena = (string)settings.GetValue("Conexion2", typeof(string));

    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.IsPostBack == false)
        {
            HiddenField1.Value = this.Request.QueryString["doc"];
            HiddenField3.Value = this.Request.QueryString["tipo"];
            HiddenField4.Value = this.Request.QueryString["clase"];
            lblTipoDocumento.Text = HiddenField1.Value;
            obtenIdDocumento(int.Parse(HiddenField3.Value),byte.Parse(HiddenField4.Value));            
        }
    }

    private void obtenIdDocumento(int iTipodoc, byte bClaseDoc)
    {
        //Usuario u = new Usuario();
        //u.idUsuario = 1;
        //u.Nombre ="vceja";
        //u.Password="14mayo1973";

        using (CoseaBussinesQA.BussinesQA c = new CoseaBussinesQA.BussinesQA(cadena, UsuarioActual.Usuario))
        {
            HiddenField2.Value = c.NuevoDocumento(iTipodoc, bClaseDoc).ToString();
        }
    }
    

    protected void btnGuardarPasos_Click(object sender, EventArgs e)
    {

    }


    //protected void btnRegresar_Click(object sender, EventArgs e)
    //{
       // Response.Redirect("WebAdmDocs.aspx");
    //}
    protected void lnkRegresar_Click(object sender, EventArgs e)
    {
        //Usuario u = new Usuario();
        //u.idUsuario = 1;
        //u.Nombre = "vceja";
        //u.Password = "14mayo1973";

        DocumentosQA docQa = new DocumentosQA();
        docQa.IdDocumento = int.Parse(HiddenField2.Value);
        docQa.IdUsuario = UsuarioActual.Usuario.idUsuario;
        docQa.Proposito = txtProposito.Text;
        docQa.Alcance = txtAlcances.Text;
        docQa.TerminosDef = txtTerminosDef.Text;
        docQa.ResponsaAuto = txtResponsa.Text;
        docQa.Contenido = txtContenido.Text;
        docQa.PasosDiagramaT = txtPasosD.Text;
        docQa.RegistrosT = txtRegistros.Text;
        docQa.AnexosT = txtAnexos.Text;
        docQa.ReferenciasT = txtReferencias.Text;

        string revision = "1.0"; //porque aqui es donde se genera el documento por primera vez.

        using (CoseaBussinesQA.BussinesQA c = new CoseaBussinesQA.BussinesQA(cadena, UsuarioActual.Usuario))
        {
            c.ActualizaDocumento(docQa, revision);
        }

        guardaImagenSolicitud(FileUpload1);

        Response.Redirect("WebAdmDocs.aspx");
    }

    private void guardaImagenSolicitud(FileUpload imgUpload)
    {
        SqlConnection connection = null;
        try
        {
            //(FileUpload)
            FileUpload img = imgUpload;
            Byte[] imgByte = null;
            if (img.HasFile && img.PostedFile != null)
            {
                //To create a PostedFile
                HttpPostedFile File = imgUpload.PostedFile;
                //Create byte Array with file len
                imgByte = new Byte[File.ContentLength];
                //force the control to load data in array
                File.InputStream.Read(imgByte, 0, File.ContentLength);
            }

            connection = new SqlConnection(cadena);

            connection.Open();
            //string sql = string.Format("update ExpedienteEmpleado set {0} = @foto where IdExpediante = @idexpediente", campo);
            string sql = "INSERT INTO DocumentoDiagrama (IdDocumento,Diagrama,FechaHora) VALUES (@id,@foto,GETDATE())";
            SqlCommand cmd = new SqlCommand(sql, connection);
            cmd.Parameters.AddWithValue("@id", int.Parse(HiddenField2.Value));
            cmd.Parameters.AddWithValue("@foto", imgByte);
            int id = Convert.ToInt32(cmd.ExecuteScalar());

        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;
            //Response.Redirect("Error.aspx?error=" + ex.Message);
            //throw new Exception(ex.Message);
        }
        finally
        {
            connection.Close();
        }
    }


}